# TVSerieScreen - This screen allows user to browse sesons of one TV-serie
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import os
import gtk
import pango
import clutter

from frontend.user_event import UserEvent
from frontend.gui.screens.screen import Screen
from frontend.gui.transitions.transition import Transition

# Widgets
from frontend.gui.widgets.text_menu import TextMenu
from frontend.gui.widgets.text_menu_item import TextMenuItem
from frontend.gui.widgets.list_indicator import ListIndicator
from frontend.gui.widgets.eyecandy_texture import EyeCandyTexture

# Video library
from frontend.medialibrary.videos import TVSeries
from frontend.medialibrary.videos import TVEpisode

class TVSerieScreen(Screen):
    """
    TVSerieScreen
    
    This screen contains all seasons of one TV-serie.
    """
    
    def __init__(self, ui, frontend, serie):
        """
        Initialize screen
        
        @param frontend: FrontendClient object
        @param theme: Theme object
        @param serie: TVSeries object
        """
        self.group = clutter.Group()
        Screen.__init__(self, ui.STAGE_WIDTH, ui.STAGE_HEIGHT)
        
        self.ui = ui
        self.frontend = frontend
        self.theme = frontend.getTheme()
        self.serie = serie
        
        # Screen Title (Displayed at the bottom left corner)
        screen_title = clutter.Label()
        screen_title.set_font_name(self.theme.getFont() + " " + str(self.ui.y(0.13)) + "px")
        screen_title.set_color(self.theme.getColor("screentitle"))
        screen_title.set_text(self.serie.get_title())
        screen_title.set_name("screen_title")
        screen_title.set_position(self.ui.x(0), self.ui.y(0.87))
        screen_title.show()
        self.group.add(screen_title)
        
        self._create_serie_cover_texture()
        self._create_season_menu()
        
        #List indicator
        self.li = ListIndicator(0.2, 0.045, self.theme, ListIndicator.VERTICAL)
        self.li.set_position(self.ui.STAGE_WIDTH - self.li.get_width() - self.ui.x(0.0659),
                             self.ui.STAGE_HEIGHT - self.ui.y(0.1953))
        self.li.set_delimiter(" | ")
        self.li.show()
        self.li.set_maximum(self.season_menu.get_number_of_items())
        self.li.set_current(1)
        self.group.add(self.li)
        
        self.group.show()
        ui.getStage().add(self.group)
        
    def _create_season_menu(self):
        """
        Create a list of available seasons
        """
        self.season_menu = TextMenu(self.theme, 
                                    self.frontend.get_configuration().show_effects())
        self.season_menu.set_row_count(1)
        self.season_menu.set_item_size(self.ui.x(0.4393), self.ui.y(0.0781))
        self.season_menu.set_position(self.ui.x(0.4978), self.ui.y(0.1563))
        
        # Create menu items based on MusicLibrary
        seasons = self.serie.get_seasons()
        seasons.sort()
        for season in seasons:
            item = TextMenuItem(self.ui.x(0.4393), self.ui.y(0.0781), self.theme, "Season " + str(season))
            item.set_userdata(season)
            self.season_menu.add_actor(item)
                
        self.season_menu.show()
        self.season_menu.set_active(True)
        self.group.add(self.season_menu)
        
    def _create_serie_cover_texture(self):
        """
        Create a texture that is displayed next to track list. This texture
        displays album cover art.
        """
        if(self.serie.has_cover_art()):
            pixbuf = gtk.gdk.pixbuf_new_from_file(self.serie.get_cover_art_url())
        else:
            pixbuf = gtk.gdk.pixbuf_new_from_file(self.theme.getImage("default_movie_art"))
        self.art = EyeCandyTexture(pixbuf, self.ui.x(0.2196), self.ui.y(0.5859))
        self.art.set_position(self.ui.x(0.16),self.ui.y(0.15))
        self.art.show()
        self.group.add(self.art)
        
    def get_group(self):
        """
        Get all actors of the screen.
        @return clutter.Group 
        """
        return self.group

    def get_type(self):
        """Return screen type."""
        return Screen.NORMAL
    
    def get_name(self):
        """Return screen name (human readble)"""
        return "TVSerieScreen"
    
    def handle_user_event(self, event):   
        """ 
        Handle screen specific user events
        @param event: UserEvent object
        """
        type = event.get_type()
        
        if type == UserEvent.NAVIGATE_UP:
            self.season_menu.move(self.season_menu.UP)
            self.li.set_current(self.season_menu.get_current_postition() + 1)
        
        elif type == UserEvent.NAVIGATE_DOWN:
            self.season_menu.move(self.season_menu.DOWN)
            self.li.set_current(self.season_menu.get_current_postition() + 1)
       
        elif type == UserEvent.NAVIGATE_SELECT:
            season = self.season_menu.get_current_menuitem().get_userdata()
            data = [self.serie, season]
            self.ui.changeScreen(self.ui.createScreen("tv-episodes", data), Transition.FORWARD)